<template>
  <div class="app-container">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getList()">
      <el-form-item>
        <el-input v-model="dataForm.matchValue" placeholder="匹配关键词" clearable />
      </el-form-item>
      <el-form-item>
        <el-button @click="getList()">查询</el-button>
        <el-button v-hasPermi="['wx:msgreplyrule:save']" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-hasPermi="['wx:msgreplyrule:delete']" type="danger" :disabled="dataListSelections.length <= 0" @click="deleteHandle()">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :key="tableKey" v-loading="listLoading" :data="list" border type="expand" style="width: 100%;" @selection-change="selectionChangeHandle">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="作用范围">
              <span>{{ props.row.appid?'当前公众号':'全部公众号' }}</span>
            </el-form-item>
            <el-form-item label="精确匹配">
              <span>{{ props.row.exactMatch?'是':'否' }}</span>
            </el-form-item>
            <el-form-item label="是否有效">
              <span>{{ props.row.status?'是':'否' }}</span>
            </el-form-item>
            <el-form-item label="备注说明">
              <span>{{ props.row.desc }}</span>
            </el-form-item>
            <el-form-item label="生效时间">
              <span>{{ props.row.effectTimeStart }}</span>
            </el-form-item>
            <el-form-item label="失效时间">
              <span>{{ props.row.effectTimeEnd }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="selection" header-align="center" align="center" width="50" />
      <el-table-column prop="ruleName" header-align="center" align="center" show-overflow-tooltip label="规则名称" />
      <el-table-column prop="matchValue" header-align="center" align="center" show-overflow-tooltip label="匹配关键词" />
      <el-table-column prop="replyType" header-align="center" align="center" :formatter="replyTypeFormat" label="消息类型" />
      <el-table-column prop="replyContent" header-align="center" align="center" show-overflow-tooltip label="回复内容" />
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页列-->
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
  </div>
</template>

<script>
import AddOrUpdate from './msg-reply-rule-add-or-update'
import { mapState } from 'vuex'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { tableInfo, crudUtil, sortUtil } from '@/utils/future/toolkit'
import { fetchWxMsgReplyRuleList, delWxMsgReplyRule } from '@/api/wx'
export default {
  components: {
    AddOrUpdate, Pagination
  },
  data() {
    const tableData = {
      dataForm: {
        matchValue: ''
      },
      dataListSelections: [],
      addOrUpdateVisible: false,
      showSearch: true, // 显示搜索条件
      queryParams: {
        scheduleName: undefined,
        createUid: undefined
      },
      promotionDate: []
    }
    return Object.assign(tableData, tableInfo)
  },
  computed: mapState({
    KefuMsgType: state => state.message.KefuMsgType
  }),

  created() {
    this.getList()
  },
  methods: {
    // 获取数据列表
    getList() {
      var that = this
      const queryParams = {}
      that.queryParams = queryParams
      crudUtil.getList(that, fetchWxMsgReplyRuleList)
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => item.ruleId)
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delWxMsgReplyRule(ids).then(response => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => this.getList()
          })
        })
      })
    },
    replyTypeFormat(row, column, cellValue) {
      return this.KefuMsgType[cellValue]
    }
  }
}
</script>
